package com.itheima.edu.info.manger.contrller;

import com.itheima.edu.info.manger.domain.Teacher;
import com.itheima.edu.info.manger.service.TeacherService;

import java.util.Scanner;

public class TeacherContrller {
    private TeacherService teacherService = new TeacherService();
    private Scanner sc = new Scanner(System.in);

    public void start() {

        teacherloop:
        while (true) {
            System.out.println("请输入你的选择：1 添加老师  2 删除老师  3 修改信息  4 查询老师信息  5 退出");
            String choose = sc.next();
            switch (choose) {
                case "1":
                    addteacher();
                    break;
                case "2":
                    deleteteacher();
                    break;
                case "3":
                    updateteacher();
                    break;
                case "4":
                    findteacher();
                    break;
                case "5":
                    System.out.println("退出系统");
                    break teacherloop;
                default:
                    System.out.println("输入错误，重新输入");
            }
        }
    }

    public void addteacher() {
        String tid;
        while (true) {
            //从键盘获取输入的信息
            System.out.println("请输入老师ID：");
            tid = sc.next();
            boolean flog = teacherService.isExcies(tid);
            if (flog) {
                System.out.println("输入id冲突");
            } else {
                break;
            }
        }

        //调用teacherservice的addteacher方法
        Teacher teacher= teacherinfo(tid);
        boolean result = teacherService.addteacher(teacher);
        //判断是否添加成功
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    public void deleteteacher() {
        String detid= inputteacher();
        teacherService.deleteid(detid);
        //提示删除成功
        System.out.println("删除成功");
    }

    public void updateteacher() {
        String uptid= inputteacher();
        Teacher newstu=teacherinfo(uptid);
        teacherService.updatateacher(uptid, newstu);
        System.out.println("修改成功");
    }

    public void findteacher() {
        Teacher[] teachers = teacherService.findteacher();
        if (teachers == null) {
            System.out.println("信息为空，重新输入");
            return;
        }
        System.out.println("教师ID\t姓名\t年龄");
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher1 = teachers[i];
            if (teacher1 != null) {
                System.out.println(teacher1.getId() + "\t\t" + teacher1.getName() + "\t\t" + teacher1.getAge());
            }
        }
    }

    public  String inputteacher(){
        String id;
        while (true) {
            //从键盘获取输入的信息
            System.out.println("请输入老师ID：");
            id = sc.next();
            boolean excie = teacherService.isExcies(id);
            if (!excie) {
                System.out.println("输入id不存在");
            } else {
                break;
            }
        }
return id;
    }
    public Teacher teacherinfo(String tid){
        System.out.println("请输入老师姓名：");
        String tname = sc.next();
        System.out.println("请输入老师年龄：");
        String tage = sc.next();
        //封装学生信息
        Teacher teacher = new Teacher();
        teacher.setId(tid);
        teacher.setName(tname);
        teacher.setAge(tage);
        return teacher;
    }
}